package com.phrp;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;

import java.util.Collections;

/**
 * @ProjectName:Backend
 * @Title: CodeGenerator
 * @Package com.phrp
 * @Description: mybatis-plus自动生成器类
 * @author SuShuChao
 * @date 2024/3/30 17:27
 * @version V1.0
 * Copyright (c) 2024, 2034279227@qq.com All Rights Reserved.
 */
public class MybatisPlusGenerator {

    public static void main(String[] args) {
        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/phrp?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai", "root", "962464").build();


        System.out.println(System.getProperty("user.dir"));

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .outputDir(System.getProperty("user.dir") + "/Managerment/src/main/java") // 生成文件输出目录
                .author("ssc") // 作者
                .disableOpenDir()
                .build();

        // 包配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.phrp") // 父包名
                .entity("entity")
                .mapper("dao")
                .service("service")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml,
                        System.getProperty("user.dir") + "/Managerment/src/main/resources/mapper")) //xml生成路径
                .build();

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .addInclude("inspection_record","report_record"
                        // "user","tenancy","payment","opinion","notice","manager_user",
                        // "live_person","house_record","house"
                ) // 需要生成的表名
                .mapperBuilder()                            //开启mapper配置
                .enableFileOverride()                       //是否覆盖
                .enableMapperAnnotation()                   //开启@mapper注解
                .formatMapperFileName("%sDao")              //格式化命名格式
                .formatXmlFileName("%sXml")                 //格式化xml命名格式
                .serviceBuilder()                           //开启service配置
                .formatServiceFileName("%sService")         //实现类格式化命名
                .formatServiceImplFileName("%sServiceImpl") //接口格式化命名
                .controllerBuilder()                        //控制层配置
                .enableFileOverride()                       //是否覆盖
                .enableRestStyle()                          //是否开启RestController
                .entityBuilder()                            //实体类配置
                .enableFileOverride()                       //是否覆盖
                .enableLombok()                             //是否使用lombok
                .build();                                   //启动


        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);
        autoGenerator.global(globalConfig);
        autoGenerator.packageInfo(packageConfig);
        autoGenerator.strategy(strategyConfig);
        autoGenerator.execute();
    }
}
